package me.sealer.ssm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.sealer.ssm.constant.Dict;
import me.sealer.ssm.constant.PermissionConstant;
import me.sealer.ssm.controller.dto.CommonResponse;
import me.sealer.ssm.controller.dto.DwzResponse;
import me.sealer.ssm.service.DwzService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sealer
 * @date 2017/06/19
 */
@Slf4j
@RestController
@RequestMapping("/urlOperations")
@Api(tags = "短网址生成服务相关api")
@RequiresPermissions(value = {PermissionConstant.URL_MGMT})
public class ShortUrlController {
    @Value("${sealer.dwzPrefix}")
    private String dwzPrefix;

    private final DwzService dwzService;

    public ShortUrlController(DwzService dwzService) {
        this.dwzService = dwzService;
    }

    @ApiOperation(value = "生成长网址对应的短网址")
    @GetMapping(value = "/genDwz", produces = MediaType.APPLICATION_JSON_VALUE)
    public CommonResponse<DwzResponse> generateDwz(@ApiParam(value = "请求参数：长url地址", required = true) @RequestParam(Dict.LONG_URL) String longUrl) {

        String shortUrl = dwzService.generateShortUrl(dwzPrefix, longUrl);
        return new CommonResponse<>(new DwzResponse(shortUrl));
    }
}
